<?php
class Roles
{
	/**
	 * 
	 * 定义右侧菜单
	 * @var array
	 */
	public static $menus = array(
		array(
			'name'=>'管理员管理',
			'submenu' => array(
				array(
					'name' => '管理帐号',
					'submenu' => array(
						array('name'=>'查看管理员','path'=>'admin/index','role'=>array(1)),
						array('name'=>'修改自己的密码','path'=>'admin/editmypwd','role'=>array(1,2,3,4)),
					),
				),
			),
		),
		array(
			'name'=>'内容管理',
			'submenu' => array(
				array(
					'name' => '企业新闻',
					'submenu' => array(
						array('name'=>'查看新闻','path'=>'news/index','role'=>array(1,2)),
						array('name'=>'查看新闻类别','path'=>'news/category','role'=>array(1)),
					),
				),
				array(
					'name' => '人才招聘',
					'submenu' => array(
						array('name'=>'查看招聘','path'=>'jobs/index','role'=>array(1,3)),
						array('name'=>'查看招聘分类','path'=>'jobs/category','role'=>array(1,3)),
					),
				),
				array(
					'name' => '产品信息',
					'submenu' => array(
						array('name'=>'查看产品','path'=>'products/index','role'=>array(1,2,4)),
						array('name'=>'查看产品类别','path'=>'products/category','role'=>array(1,4)),
					),
				),
				array(
					'name' => '站内链接',
					'submenu' => array(
						array('name'=>'查看','path'=>'links/index','role'=>array(1,2,4,)),
					),
				),
			),
		),
		array(
			'name'=>'互动信息管理',
			'submenu' => array(
				array(
					'name' => '问卷信息',
					'submenu' => array(
						array('name'=>'查看','path'=>'feedback/index','role'=>array(1,4)),
					),
				),
			),
		),
	);
	/**
	 * 
	 * 定义网站角色和权限
	 * @var 
	 */
	public static $roles = array(
		1 => '超级管理员',
		2 => '网站编辑',
		3 => '人事部',
		4 => '市场部',
	);
	/**
	 * 
	 * 根据role_id获得应显示的菜单
	 * @param int $role_id
	 */
	public static function getrole($role_id)
	{
		$role = Yii::app()->cache->get('left_menu_group_role'.$role_id);
		if($role === false)
		{
			$role = self::$menus;
			foreach ($role as $k1=>$lv1)
			{
				foreach ($lv1['submenu'] as $k2=>$lv2)
				{
					foreach ($lv2['submenu'] as $k3=>$role_items)
					{
						if(!in_array($role_id, $role_items['role']))
						{
							unset($role[$k1]['submenu'][$k2]['submenu'][$k3]);
						}
					}
					if(empty($role[$k1]['submenu'][$k2]['submenu']))
					{
						unset($role[$k1]['submenu'][$k2]);
					}
				}
				if(empty($role[$k1]['submenu']))
				{
					unset($role[$k1]);
				}
			}
			Yii::app()->cache->set('left_menu_group_role'.$role_id,$role,3600);
		}
		return $role;
	}
}